package com.qding.park.manager.service.statis.facade.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import com.qding.park.common.core.constant.MongodbConstant;
import com.qding.park.common.core.exceptions.BaseException;
import com.qding.park.common.utils.DateUtils;
import com.qding.park.common.utils.ListUtil;
import com.qding.park.common.utils.string.StringUtil;
import com.qding.park.manager.common.pojo.ParkDataResult;
import com.qding.park.manager.facade.statis.service.MonthStatisFacade;
import com.qding.park.manager.pojo.example.NcMonthRentExample;
import com.qding.park.manager.pojo.noncore.NcMonthRent;

/**
  * @ClassName: MonthStatisFacadeImpl
  * @Description: 月租单统计
  * @author Comsys-Administrator
  * @date 2016年10月11日 下午2:01:54
  *
  */
@Service("monthStatisFacade")
public class MonthStatisFacadeImpl implements MonthStatisFacade {
    private static final Logger _log = LoggerFactory.getLogger(MonthStatisFacadeImpl.class); 
	
	@Autowired
	private  MongoTemplate mongoTemplate;
	
	
	/*
	  * <p>Title: list</p>
	  * <p>Description:月租单统计查询 </p>
	  * @param example
	  * @param jumpPage
	  * @param pageLength
	  * @return
	  * @throws BaseException
	  * @see com.qding.park.manager.facade.statis.service.MonthStatisFacade#list(com.qding.park.manager.pojo.example.TbMonthRentExample, int, int)
	  */
	
	@Override
	public ParkDataResult list(NcMonthRentExample example, int jumpPage, int pageLength) throws BaseException {
		String tablename_monthrent = MongodbConstant.TABLENAME_MONTHRENT;
		
		Criteria matchCrieria = new Criteria();
		matchCrieria.where("_id").not();
		
		
		if (example.getPayStartTime() != null && example.getPayEndTime() != null) {
			matchCrieria.andOperator(Criteria.where("rechargeDate").gte(example.getPayStartTime()),
					Criteria.where("rechargeDate").lte(example.getPayEndTime()));
		} else if (example.getPayStartTime()!= null) {
			matchCrieria.andOperator(Criteria.where("rechargeDate").gte(example.getPayStartTime()));
		} else if (example.getPayEndTime() != null) {
			matchCrieria.andOperator(Criteria.where("rechargeDate").lte(example.getPayEndTime()));
		}
		
		if (!StringUtils.isBlank(example.getCarNum())) {
//			matchCrieria.and("carNum").is(example.getCarNum());
			matchCrieria.and("carNum").regex(".*"+example.getCarNum()+".*");
		}
		
		if (!StringUtils.isBlank(example.getOrderSource())) {
			matchCrieria.and("orderSource").is(Integer.valueOf(example.getCarNum()));
		}
		
//		if(!StringUtils.isBlank(example.getParkId())){
//			matchCrieria.and("parkID").is(Integer.valueOf(example.getParkId()));
//		}
		
		if (!StringUtils.isBlank(example.getParkId())&&!ListUtil.isEmpty(example.getInParks())) {
			matchCrieria.andOperator(Criteria.where("parkID").is(Integer.valueOf(example.getParkId())),
					Criteria.where("parkID").in(example.getInParks()));
		}else if(!StringUtils.isBlank(example.getParkId())){
			matchCrieria.and("parkID").is(Integer.valueOf(example.getParkId()));
		}else if(!ListUtil.isEmpty(example.getInParks())){
			matchCrieria.and("parkID").in(example.getInParks());
		}
		Query query=Query.query(matchCrieria);
		query.with(new Sort(new Order(Direction.DESC,"rechargeDate")));
		query.skip((jumpPage - 1) * pageLength);
		query.limit(pageLength);
		
		
		Query querytotal=Query.query(matchCrieria);
		
		List<NcMonthRent> ncMonthRentlist= mongoTemplate.find(query, NcMonthRent.class, tablename_monthrent);
		
		long total= mongoTemplate.count(querytotal, tablename_monthrent);
		
		List dataList = new ArrayList();
		int totalFee=0;
		for(NcMonthRent ncMonthRent:ncMonthRentlist){
			Map<String, Object> map = bean2map(ncMonthRent);
//			map.put("orderId", ncMonthRent.getPayOrderID());
//			map.put("orderStatus", DateUtils.formatDate(ncMonthRent.getCreate_date(),DateUtils.DATE_FORMAT_DATETIME)+" 成功");
//			map.put("carNum", ncMonthRent.getCarNum());
//			map.put("tel", ncMonthRent.getTel());
//			map.put("parkName", ncMonthRent.getParkName());
////			map.put("receivableParkingFee", ncMonthRent.getShouldPaymentAmount());
//			map.put("receivedFee", ncMonthRent.getPaymentAmount());
////			map.put("differenceFee", ncMonthRent.getShouldPaymentAmount()-ncMonthRent.getPaymentAmount());
////			map.put("monthlyStartTime", ncMonthRent.getMonthlyStartTime()==null?"":DateUtils.formatDate(ncMonthRent.getMonthlyStartTime(),DateUtils.DATE_FORMAT_DATETIME));
//			map.put("monthlyEndTime", ncMonthRent.getMonthlyEndTime()==null?"":DateUtils.formatDate(ncMonthRent.getMonthlyEndTime(),DateUtils.DATE_FORMAT_DATETIME));
////			map.put("payTime", ncMonthRent.getRechargeDate()==null?"":DateUtils.formatDate(ncMonthRent.getRechargeDate(),DateUtils.DATE_FORMAT_DATETIME));
//			map.put("payType", ncMonthRent.getOrderSource());
			totalFee=totalFee+ncMonthRent.getPaymentAmount();
			dataList.add(map);
		}
		
		
		ParkDataResult parkDataResult=ParkDataResult.createObjDate();
		parkDataResult.putDataOnObj("currentPage", jumpPage);
		parkDataResult.putDataOnObj("numPerPage", pageLength);
		parkDataResult.putDataOnObj("totalCount", total);
		parkDataResult.putDataOnObj("hast", jumpPage > 1 ? true : false);
		parkDataResult.putDataOnObj("next", jumpPage * pageLength < total ? true : false);
		parkDataResult.putDataOnObj("totalFee", totalFee);
		parkDataResult.putDataOnObj("list",dataList);
		return parkDataResult;
	}

	@Override
	public List<NcMonthRent> exportExcel(NcMonthRentExample example) {
		String tablename_monthrent = MongodbConstant.TABLENAME_MONTHRENT;
		
		Criteria matchCrieria = new Criteria();
//		matchCrieria.where("_id").not();
		
		
		if (example.getPayStartTime() != null && example.getPayEndTime() != null) {
			matchCrieria.andOperator(Criteria.where("rechargeDate").gte(example.getPayStartTime()),
					Criteria.where("rechargeDate").lte(example.getPayEndTime()));
		} else if (example.getPayStartTime()!= null) {
			matchCrieria.andOperator(Criteria.where("rechargeDate").gte(example.getPayStartTime()));
		} else if (example.getPayEndTime() != null) {
			matchCrieria.andOperator(Criteria.where("rechargeDate").lte(example.getPayEndTime()));
		}
		
//		if(!StringUtils.isBlank(example.getParkId())){
//			matchCrieria.and("parkID").is(Integer.valueOf(example.getParkId()));
//		}
		
		if (!StringUtils.isBlank(example.getParkId())&&!ListUtil.isEmpty(example.getInParks())) {
			matchCrieria.andOperator(Criteria.where("parkID").is(Integer.valueOf(example.getParkId())),
					Criteria.where("parkID").in(example.getInParks()));
		}else if(!StringUtils.isBlank(example.getParkId())){
			matchCrieria.and("parkID").is(Integer.valueOf(example.getParkId()));
		}else if(!ListUtil.isEmpty(example.getInParks())){
			matchCrieria.and("parkID").in(example.getInParks());
		}
		
		if (!StringUtils.isBlank(example.getCarNum())) {
			matchCrieria.and("carNum").is(example.getCarNum());
		}
		
		if (!StringUtils.isBlank(example.getOrderSource())) {
			matchCrieria.and("orderSource").is(Integer.valueOf(example.getCarNum()));
		}
		Query query=Query.query(matchCrieria);
		query.with(new Sort(new Order(Direction.DESC,"rechargeDate")));
		_log.info(query.toString());
		
		
		List<NcMonthRent> ncMonthRentlist= mongoTemplate.find(query, NcMonthRent.class, tablename_monthrent);
		
		
		return ncMonthRentlist;
	}

	private  Map<String, Object> bean2map(NcMonthRent ncMonthRent){
		Map<String, Object> map = new HashMap<String, Object>();
		if(ncMonthRent==null){
			return map;
		}
		map.put("orderId", ncMonthRent.getPayOrderID());
		map.put("orderStatus", DateUtils.formatDate(ncMonthRent.getRechargeDate(),DateUtils.DATE_FORMAT_DATETIME)+" 成功");
		map.put("carNum", ncMonthRent.getCarNum());
		map.put("tel",StringUtil.isBlank( ncMonthRent.getTel())?"":ncMonthRent.getTel());
		map.put("parkName", ncMonthRent.getParkName());
//		map.put("receivableParkingFee", ncMonthRent.getShouldPaymentAmount());
		map.put("receivedFee", ncMonthRent.getPaymentAmount());
//		map.put("differenceFee", ncMonthRent.getShouldPaymentAmount()-ncMonthRent.getPaymentAmount());
//		map.put("monthlyStartTime", ncMonthRent.getMonthlyStartTime()==null?"":DateUtils.formatDate(ncMonthRent.getMonthlyStartTime(),DateUtils.DATE_FORMAT_DATETIME));
//		map.put("monthlyEndTime", ncMonthRent.getMonthlyEndTime()==null?"":DateUtils.formatDate(ncMonthRent.getMonthlyEndTime(),DateUtils.DATE_FORMAT_DATETIME));
		map.put("monthlyEndTime", ncMonthRent.getMonthlyEndTime()==null?0:ncMonthRent.getMonthlyEndTime().getTime()/1000);
//		map.put("payTime", ncMonthRent.getRechargeDate()==null?"":DateUtils.formatDate(ncMonthRent.getRechargeDate(),DateUtils.DATE_FORMAT_DATETIME));
		map.put("payType", ncMonthRent.getOrderSource());
		
		return map;
	}
	
	@Override
	public ParkDataResult orderInfoById(String orderId) {
		String tablename_monthrent = MongodbConstant.TABLENAME_MONTHRENT;
		Criteria matchCrieria = new Criteria();
		matchCrieria.and("payOrderID").is(orderId);
		_log.info(matchCrieria.getCriteriaObject().toString());
		Query query=Query.query(matchCrieria);
		NcMonthRent  month=mongoTemplate.findOne(query, NcMonthRent.class, tablename_monthrent);
		
		ParkDataResult parkDataResult=ParkDataResult.createObjDate();
		parkDataResult.putMapOnObj(bean2map(month));
		
		return parkDataResult;
	}

}
